
package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ActionEvent;

@ManagedBean
@SessionScoped
public class LopHocBean {

    private String msLop;
    private String tenLop;
    private String tenGVCN;
 
    public LopHocBean() {
    }

    public LopHocBean(String msLop, String tenLop, String tenGVCN) {
        this.msLop = msLop;
        this.tenLop = tenLop;
        this.tenGVCN = tenGVCN;
    }

    public String getMsLop() {
        return msLop;
    }

    public void setMsLop(String msLop) {
        this.msLop = msLop;
    }

    public String getTenGVCN() {
        return tenGVCN;
    }

    public void setTenGVCN(String tenGVCN) {
        this.tenGVCN = tenGVCN;
    }

    public String getTenLop() {
        return tenLop;
    }

    public void setTenLop(String tenLop) {
        this.tenLop = tenLop;
    }

    //Liet ke cac Record cua bang cua lop hoc de hien thi len table
    public List<LopHocBean> getAllLophoc(){
        ArrayList<LopHocBean> lst = new ArrayList<LopHocBean>();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JsfAccess", "sa","123");
            ResultSet rs = con.createStatement().executeQuery("select * from Lophoc");
            while(rs.next()){
                LopHocBean lh = new LopHocBean(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3));
                lst.add(lh);
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lst;
    }

//    Action dung de insert mot mau tin moi
//    return success neu thanh cong
    public String InsertNew(){
        try {
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JsfAccess", "sa","123");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://localhost:1433; databaseName=JsfAccess";
            Connection con = DriverManager.getConnection(url,"sa","123");
            String sql = "insert into Lophoc values(?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, msLop);
            ps.setString(2, tenLop);
            ps.setString(3, tenGVCN);
            int x = ps.executeUpdate();
            if(x>0){
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "failed";
    }

    //Action delete dung de xoa mau tin duoc chon
    public void deleteAction(ActionEvent evt){
        //we get the table object
        HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource());
        //We get the object on the selected line
        Object o = table.getRowData();
        LopHocBean selLh = (LopHocBean) o;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JsfAccess", "sa","123");
            String sql = "delete Lophoc where msLop=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, selLh.msLop);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Action dung de chon mau tinc an cap nhat
    //Action evt
    public void editBook(ActionEvent evt){
        //We get the table Object
        HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource());
	// We get the object on the selected line.
	Object o = table.getRowData();
        LopHocBean lh = (LopHocBean) o;
        this.msLop = msLop;
        this.tenLop = tenLop;
        this.tenGVCN = tenGVCN;
    }

    //Method to get the HtmlDataTbale
    private HtmlDataTable getParentDatatable(UIComponent compo){
        if(compo == null){
            return null;
        }
        if(compo instanceof HtmlDataTable){
            return (HtmlDataTable)compo;
        }
        return getParentDatatable(compo.getParent());
        }

    //Cap nhat thong tin duoc chon len CSDL
    //@return
    public String update(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JsfAccess", "sa","123");
            String sql = "update Lophoc set tenLop=?, tenGVCN=? where msLop=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, tenLop);
            ps.setString(2, tenGVCN);
            ps.setString(3, msLop);
            int x = ps.executeUpdate();
            if(x > 0){
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "failed";
    }
}
